package com.vancouver.knows.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pangu.util.StringUtil;
import com.vancouver.knows.business.bean.CategoryInfo;
import com.vancouver.knows.business.bean.UserPostInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Chris Li on 2016-07-12.
 */
public class FetchUserPostsHandler extends ACheckableJsonParser {

    public static final String KEY_POSTS = "posts";
    public static final String KEY_TOP_CATEGORIES = "top_categories";

    private HashMap<String, Object> resultMap = new HashMap<String, Object>();

    @Override
    protected void parserContent(JSONObject content) throws Exception {
        String posts = content.getString("posts");
        if (StringUtil.isEmptyString(posts)) {
            resultMap.put(KEY_POSTS, new ArrayList<UserPostInfo>());
        } else {
            List<UserPostInfo> userPostInfoList = JSON.parseArray(posts, UserPostInfo.class);
            resultMap.put(KEY_POSTS, userPostInfoList);
        }

        String topCategories = content.getString("top_categories");
        if (StringUtil.isEmptyString(topCategories)) {
            resultMap.put(KEY_TOP_CATEGORIES, new ArrayList<CategoryInfo>());
        } else {
            List<CategoryInfo> topCategoriesList = JSON.parseArray(topCategories, CategoryInfo.class);
            resultMap.put(KEY_TOP_CATEGORIES, topCategoriesList);
        }
    }

    public HashMap<String, Object> getResultMap() {
        return resultMap;
    }
}
